<template>
  <div class="comment" v-if="hasComment">
    <p class="commentUserName"><strong>{{ commentList.userName || '未知用户' }}</strong></p>
    <header class="commentHeader">
      <img
          v-if="!isNull(commentList.userHeadPortraitUrl)"
          :src="`${globalConst.staticResourceUrl}img/${commentList.userHeadPortraitUrl}`"
          alt="用户头像"
          class="avatar"
          width="50"
          height="50"
      />
      <h4 v-else class="commentNoAvatarHint">该用户未设置头像</h4>
      <div class="commentTimeContainer">
        <p class="commentTime">{{ formatDate(commentList.originalComment.commentTime) }}</p>
      </div>
    </header>
    <article class="commentContent">
      <p class="commentText">{{ commentList.originalComment.comment }}</p>
      <button @click="handleReply(commentList)" v-if="isLogin" class="commentReplyBtn">回复(评论id:
        {{
          commentList.originalComment.commentId
        }})
      </button>
    </article>
    <ul v-if="commentList.originalComment.sumComment && commentList.originalComment.sumComment.length > 0"
        class="commentSubList">
      <comment-item
          v-for="subComment in commentList.originalComment.sumComment"
          :key="subComment.originalComment.commentId"
          :comment-list="subComment"
          :has-comment="true"
      />
    </ul>
  </div>
  <div v-else class="commentEmptyContainer">
    <h3 class="commentEmptyHint">暂时还没有评论呢</h3>
  </div>
</template>

<script setup lang="ts">
import {defineProps, ref} from 'vue'
import emitter from '@utils/emitter.ts'
import {globalConst} from "@constants/globalConst.ts";
import {getUser, getUserToken} from "@stores/localStorage.ts";
import {isNull} from "@utils/stringUtil.ts";
import {CommentWithUser} from "@models/CommentWithUser.ts";

const {commentList, hasComment} = defineProps(['commentList', 'hasComment'])
commentList as Array<CommentWithUser>

const isLogin = ref(getUser().hasUserMsg() && !isNull(getUserToken())) // 判断是否登录
const formatDate = (milliseconds: number) => {
  if (!milliseconds) return '无时间信息'
  const date = new Date(milliseconds)
  return date.toLocaleString()
}

function handleReply(c) {
  const commentId = c.originalComment.commentId
  if (commentId) {
    const string = new Array<string | number>()
    string.push(c.userName, c.originalComment.comment, commentId)
    console.log('回复事件被触发:', string)
    emitter.emit('reply', string) // 直接传递更多有用信息
  }
}

</script>
<style scoped>
/* ======== 评论容器 ======== */
.comment {
  width: 85%;
  margin: 1.5rem auto;
  padding: 1.5rem 2rem;
  border-radius: 16px;
  background: rgba(40, 0, 0, 0.35);
  backdrop-filter: blur(8px);
  box-shadow: 0 4px 20px rgba(255, 42, 109, 0.15);
  border: 1px solid rgba(255, 42, 109, 0.2);
  transition: all 0.3s ease;
  animation: fadeIn 0.6s ease-out;
}

.comment:hover {
  background: rgba(50, 0, 0, 0.4);
  transform: translateY(-3px);
  box-shadow: var(--shadow-primary);
  border-color: rgba(255, 42, 109, 0.3);
}

/* ======== 评论头部 ======== */
.commentHeader {
  display: flex;
  align-items: center;
  gap: 1rem;
  margin-bottom: 1rem;
}

.avatar {
  border-radius: 50%;
  border: 2px solid rgba(255, 42, 109, 0.4);
  width: 50px;
  height: 50px;
  object-fit: cover;
  transition: all 0.3s ease;
  box-shadow: 0 0 8px rgba(255, 42, 109, 0.2);
}

.avatar:hover {
  transform: scale(1.08);
  border-color: var(--primary-red);
  box-shadow: 0 0 12px rgba(255, 42, 109, 0.4);
}

/* ======== 用户信息 ======== */
.commentUserName {
  font-size: 1.1em;
  font-weight: 600;
  background: linear-gradient(135deg, var(--primary-red), var(--primary-purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 0.3rem;
}

.commentTime {
  font-size: 0.85em;
  color: var(--text-secondary);
  margin-top: 0.2rem;
}

.commentNoAvatarHint {
  font-size: 0.9em;
  color: var(--text-muted);
  font-style: italic;
}

/* ======== 评论内容 ======== */
.commentContent {
  margin-left: 3.2rem;
}

.commentText {
  color: var(--text-primary);
  line-height: 1.6;
  margin: 0.8rem 0;
  word-wrap: break-word;
  font-size: 1em;
}

/* ======== 回复按钮 ======== */
.commentReplyBtn {
  border-radius: 10px;
  border: 2px solid transparent;
  padding: 0.5rem 1rem;
  font-size: 0.9em;
  font-weight: 600;
  background: linear-gradient(135deg, var(--primary-red), var(--primary-purple));
  color: var(--text-primary);
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
  box-shadow: var(--shadow-primary);
}

.commentReplyBtn::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s ease;
}

.commentReplyBtn:hover::before {
  left: 100%;
}

.commentReplyBtn:hover {
  transform: translateY(-2px);
  box-shadow: var(--shadow-glow);
  border-color: var(--primary-red-light);
}

/* ======== 子评论列表 ======== */
.commentSubList {
  margin-left: 2rem;
  padding-left: 0;
  list-style: none;
  border-left: 2px solid rgba(255, 42, 109, 0.3);
}

/* ======== 子评论项 ======== */
.commentSubList .comment {
  width: 100%;
  background: rgba(30, 0, 0, 0.25);
  box-shadow: none;
  backdrop-filter: blur(4px);
  border-radius: 0;
  margin-top: 1rem;
  padding: 1rem 0 1rem 1.5rem;
  border: none;
  border-left: 2px solid rgba(255, 42, 109, 0.2);
  transition: all 0.3s ease;
}

.commentSubList .comment:hover {
  background: rgba(40, 0, 0, 0.3);
  transform: translateX(5px);
  border-left-color: rgba(255, 42, 109, 0.4);
}

/* ======== 空状态 ======== */
.commentEmptyContainer {
  text-align: center;
  padding: 3rem 2rem;
  border-radius: 16px;
  background: rgba(26, 26, 26, 0.5);
  backdrop-filter: blur(8px);
  border: 1px solid var(--border-secondary);
  margin: 2rem auto;
  max-width: 600px;
}

.commentEmptyHint {
  font-size: 1.3em;
  font-weight: 600;
  background: linear-gradient(135deg, var(--primary-red), var(--primary-purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  margin-bottom: 1rem;
}

/* ======== 动画效果 ======== */
@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(15px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ======== 响应式设计 ======== */
@media (max-width: 768px) {
  .comment {
    width: 90%;
    padding: 1.2rem 1.5rem;
    margin: 1rem auto;
  }
  
  .commentHeader {
    gap: 0.8rem;
  }
  
  .avatar {
    width: 45px;
    height: 45px;
  }
  
  .commentContent {
    margin-left: 2.5rem;
  }
  
  .commentSubList {
    margin-left: 1.5rem;
  }
  
  .commentSubList .comment {
    padding: 0.8rem 0 0.8rem 1rem;
  }
}

@media (max-width: 480px) {
  .comment {
    width: 95%;
    padding: 1rem;
  }
  
  .commentHeader {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
  
  .avatar {
    width: 40px;
    height: 40px;
  }
  
  .commentContent {
    margin-left: 0;
  }
  
  .commentSubList {
    margin-left: 1rem;
  }
  
  .commentSubList .comment {
    padding: 0.8rem 0 0.8rem 0.8rem;
  }
  
  .commentEmptyContainer {
    padding: 2rem 1rem;
  }
  
  .commentEmptyHint {
    font-size: 1.1em;
  }
}
</style>
